package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.Agent;
import cc.yugu.yhdw.common.domain.hdw.AgentHdwSetting;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.biz.client.ClientBizUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgentHdwSettingMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 运营商设置业务
 *
 * @Date:Created in 2018/7/5 16:20
 * @Author:
 * @Modified By: zjn 2018/7/5 16:20
 */
@Service
public class AgentHdwSettingService extends AbstractService {
    @Autowired
    AgentHdwSettingMapper agentHdwSettingMapper;
    @Autowired
    AgentMapper agentMapper;
    @Autowired
    AppConfig config;
    /**
    * 分页查询
    *
    * @Param: [agentHdwSetting]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/5  16:20
    */
    public Page findPage(AgentHdwSetting agentHdwSetting) {
        Page page = agentHdwSetting.buildPage();
        page.setTotalItems(agentHdwSettingMapper.findPageCount(agentHdwSetting));
        agentHdwSetting.setBeginIndex(page.getOffset());

        List<AgentHdwSetting> agentHdwSettingList = agentHdwSettingMapper.findPageResult(agentHdwSetting);
        page.setResult(agentHdwSettingList);

        for (AgentHdwSetting a : agentHdwSettingList) {
            Agent agent = agentMapper.find(a.getId());
            if (agent != null && agent.getAgentName() != null) {
                a.setAgentName(agent.getAgentName());
            }
        }

        return page;
    }
    /**
    * 查询
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.AgentHdwSetting
    * @Author By: zjn 2018/7/5  16:20
    */
    public AgentHdwSetting find(int id) {
        // 根据主键查询
        AgentHdwSetting agentHdwSetting = agentHdwSettingMapper.find(id);
        Agent agent = agentMapper.find(id);
        if (agent != null && agent.getAgentName() != null) {
            agentHdwSetting.setAgentName(agent.getAgentName());
        }
        return agentHdwSetting;
    }
    /**
    * 插入
    * 
    * @Param: [entity]
    * @Return: int
    * @Author By: zjn 2018/7/5  16:21
    */ 
    public int insert(AgentHdwSetting entity) {
        return agentHdwSettingMapper.insert(entity);
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  16:22
    */ 
    public ExtResult update(AgentHdwSetting entity) {
        // 根据主键更新 电话和每次换电金额
        int total = agentHdwSettingMapper.update(entity);
        if (total < 1) {
            return ExtResult.failResult("修改失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 根据主键删除
    *
    * @Param: [id]
    * @Return: int
    * @Author By: zjn 2018/7/5  16:23
    */
    public int delete(int id) {
        return agentHdwSettingMapper.delete(id);
    }
}
